﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;


namespace Diplomova_prace
{
    public partial class SelectedTask : Form
    {
        string[] Tasks;
        int selectedIndexTask;
        string[] TasksWithPath;
        public int GetSelectedTask
        {
            get { return selectedIndexTask; }
        }

        public SelectedTask(string[] Tasks,string[] TasksWithPath)
        {
            this.Tasks = Tasks;
            this.TasksWithPath = TasksWithPath;
            InitializeComponent();
            SelectTasks_DataGridView.CurrentCell = null;
            FillListView();
            UpdateFont();
        }

        private void UpdateFont()
        {
            //Change cell font
            foreach (DataGridViewColumn c in SelectTasks_DataGridView.Columns)
            {
                c.DefaultCellStyle.Font = new Font(" ", 15F, GraphicsUnit.Pixel);
            }
        }
        private void FillListView()
        {
            SelectTasks_DataGridView.RowCount = Tasks.Length;
            for (int i = 0; i < SelectTasks_DataGridView.ColumnCount;i++)
                for (int j = 0; j < SelectTasks_DataGridView.RowCount; j++)
                {
                    if (i == 0)
                        SelectTasks_DataGridView[i, j].Value = j+1;
                    else if (i == 1)
                    {
                        SelectTasks_DataGridView[i, j].Value = Tasks[j];
                        if (Tasks[j].Contains(MainMenu.inValidProperty))
                            SelectTasks_DataGridView.Rows[j].DefaultCellStyle.BackColor = Color.FromArgb(255, 100, 100);
                        else
                            SelectTasks_DataGridView.Rows[j].DefaultCellStyle.BackColor = Color.FromArgb(255, 255, 255);
                    }
                }
        }

        private void SelectTask()
        {
            if (SelectTasks_DataGridView.CurrentCell.RowIndex > -1)
            {
                if (!SelectTasks_DataGridView[1, SelectTasks_DataGridView.CurrentCell.RowIndex].Value.ToString().Contains("Nevalidní"))
                {
                    selectedIndexTask = SelectTasks_DataGridView.CurrentCell.RowIndex;
                    this.DialogResult = System.Windows.Forms.DialogResult.OK;
                }
            }
        }   

        private void SelectTasks_DataGridView_SelectionChanged(object sender, EventArgs e)
        {
            selectedIndexTask = SelectTasks_DataGridView.CurrentCell.RowIndex;
        }

        private void SelectTasks_DataGridView_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Right)
                {
                    DataGridViewCell clickedCell = (sender as DataGridView).Rows[e.RowIndex].Cells[e.ColumnIndex];
                    this.SelectTasks_DataGridView.CurrentCell = clickedCell;
                    var relativeMousePosition = SelectTasks_DataGridView.PointToClient(Cursor.Position);
                    this.contextMenuStrip1.Show(SelectTasks_DataGridView, relativeMousePosition);
                }
            }
        }

        private void SelectTasks_DataGridView_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1 && e.ColumnIndex != -1)
            {
                if (e.Button == System.Windows.Forms.MouseButtons.Left)
                {
                    SelectTask();
                }
            }
        }

        private void smazatÚlohuToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("Opravdu chcete vybranou úlohu smazat?", "Smazání cvičné úlohy", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
            {
                File.Delete(TasksWithPath[selectedIndexTask]);
                List<string> tmp = TasksWithPath.ToList();
                tmp.RemoveAt(selectedIndexTask);
                TasksWithPath = tmp.ToArray();
                List<string> tmp1 = Tasks.ToList();
                tmp1.RemoveAt(selectedIndexTask);
                Tasks = tmp1.ToArray();
                SelectTasks_DataGridView.DataSource = null;
                FillListView();
            }
        }

        private void SelectTasks_DataGridView_KeyDown(object sender, KeyEventArgs e)
        {
             if (e.KeyData == System.Windows.Forms.Keys.Enter)
                 SelectTask();
             if (e.KeyData == System.Windows.Forms.Keys.Delete)
             {
                 if ((sender as DataGridView).CurrentCell.ColumnIndex == 0)
                     (sender as DataGridView).CurrentCell = (sender as DataGridView)[(sender as DataGridView).CurrentCell.ColumnIndex + 1, (sender as DataGridView).CurrentCell.RowIndex];
                 var relativeMousePosition = ((sender as DataGridView).GetCellDisplayRectangle((sender as DataGridView).CurrentCell.ColumnIndex, (sender as DataGridView).CurrentCell.RowIndex, true));
                 this.contextMenuStrip1.Show(SelectTasks_DataGridView, new Point (relativeMousePosition.X+relativeMousePosition.Width/2,relativeMousePosition.Y));
             }
        }






    }
}
